package com.ruoyi.chain.purchase.service.impl;

import com.ruoyi.chain.cooperation.domain.ChainCooperationCommodity;
import com.ruoyi.chain.cooperation.service.IChainCooperationCommodityService;
import com.ruoyi.chain.order.domain.ChainOrder;
import com.ruoyi.chain.order.service.IChainOrderService;
import com.ruoyi.chain.purchase.domain.ChainPurchase;
import com.ruoyi.chain.purchase.mapper.ChainPurchaseMapper;
import com.ruoyi.chain.purchase.service.IChainPurchaseService;
import com.ruoyi.chain.stock.service.IChainStockService;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 采购Service业务层处理
 *
 * @author Shawn
 * @date 2024-02-26
 */
@Service
public class ChainPurchaseServiceImpl implements IChainPurchaseService {
    @Autowired
    private ChainPurchaseMapper chainPurchaseMapper;

    @Autowired
    private IChainStockService iChainStockService;

    @Autowired
    private IChainOrderService iChainOrderService;

    @Autowired
    private IChainCooperationCommodityService iChainCooperationCommodityService;



    /**
     * 查询采购
     *
     * @param id 采购主键
     * @return 采购
     */
    @Override
    public ChainPurchase selectChainPurchaseById(Long id) {
        return chainPurchaseMapper.selectChainPurchaseById(id);
    }

    /**
     * 查询采购列表
     *
     * @param chainPurchase 采购
     * @return 采购
     */
    @Override
    public List<ChainPurchase> selectChainPurchaseList(ChainPurchase chainPurchase) {
        return chainPurchaseMapper.selectChainPurchaseList(chainPurchase);
    }

    /**
     * 新增采购
     *
     * @param chainPurchase 采购
     * @return 结果
     */
    @Override
    public int insertChainPurchase(ChainPurchase chainPurchase) {
        if (chainPurchase.getQuantity() <= 0) {
            throw new ServiceException("采购数量必须大于0");
        }
        chainPurchase.setCreatedBy(String.valueOf(ShiroUtils.getUserId()));
        chainPurchase.setCreatedTime(new Date());
        chainPurchase.setAuditType(0l);
        chainPurchase.setTransportStatus(1l);
        checkCommodityInventory(chainPurchase);
        return chainPurchaseMapper.insertChainPurchase(chainPurchase);
    }

    /**
     * 修改采购
     *
     * @param chainPurchase 采购
     * @return 结果
     */
    @Override
    public int updateChainPurchase(ChainPurchase chainPurchase) {
        if (chainPurchase.getQuantity() <= 0) {
            throw new ServiceException("采购数量必须大于0");
        }
        return chainPurchaseMapper.updateChainPurchase(chainPurchase);
    }


    /**
     * 批量删除采购
     *
     * @param ids 需要删除的采购主键
     * @return 结果
     */
    @Override
    public int deleteChainPurchaseByIds(String ids) {
        String[] array = Convert.toStrArray(ids);
        for (String id : array) {
            checkOperatePurchase(Long.parseLong(id));
        }
        return chainPurchaseMapper.deleteChainPurchaseByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除采购信息
     *
     * @param id 采购主键
     * @return 结果
     */
    @Override
    public int deleteChainPurchaseById(Long id) {
        return chainPurchaseMapper.deleteChainPurchaseById(id);
    }

    @Override
    public int audit(ChainPurchase chainPurchase) {
        //如果审核成功
        if (chainPurchase.getAuditType() == 3) {
            auditSuccess(chainPurchase);
        }
        return chainPurchaseMapper.updateChainPurchase(chainPurchase);
    }

    /**
     * 审核通过
     *
     * @param chainPurchase
     */
    private void auditSuccess(ChainPurchase chainPurchase) {
        ChainPurchase purchase = chainPurchaseMapper.selectChainPurchaseById(chainPurchase.getId());
        ChainCooperationCommodity cooperationCommodity = checkCommodityInventory(purchase);
        if (null != cooperationCommodity) {
            cooperationCommodity.setQuantity(cooperationCommodity.getQuantity() - purchase.getQuantity());
            cooperationCommodity.setCreatedBy(null);
            iChainCooperationCommodityService.updateChainCooperationCommodityUncheck(cooperationCommodity);
        }
        //创建订单
        ChainOrder chainOrder = new ChainOrder();
        chainOrder.setAmount(purchase.getAmount());
        chainOrder.setPurchaseId(purchase.getId());
        chainOrder.setCommodityId(purchase.getCommodityId());
        //出账
        chainOrder.setType(2l);
        chainOrder.setCreatedBy(String.valueOf(ShiroUtils.getUserId()));
        chainOrder.setCreatedTime(new Date());
        iChainOrderService.insertChainOrder(chainOrder);
    }

    @Override
    public int transport(ChainPurchase chainPurchase) {
        //商品已送达
        if (chainPurchase.getTransportStatus() == 4) {
            /*ChainPurchase purchase = chainPurchaseMapper.selectChainPurchaseById(chainPurchase.getId());
            if (null == purchase) {
                throw new ServiceException("采购数据不存在");
            }
            iChainStockService.updateInventory(purchase.getCommodityId(), purchase.getQuantity());*/
            //送达后设置检验状态为待检验
            chainPurchase.setInspectionStatus(0);
        }
        return chainPurchaseMapper.updateChainPurchase(chainPurchase);
    }

    @Override
    public int inspect(ChainPurchase chainPurchase) {
        //如果是待审核状态，则出发审核流程
        if (chainPurchase.getInspectionStatus() == 0) {
            inspectPurchase(chainPurchase);
        }
        return chainPurchaseMapper.updateChainPurchase(chainPurchase);
    }

    /**
     * 审核通过
     *
     * @return
     */
    public void inspectPurchase(ChainPurchase chainPurchase) {
        //获取最新采购单数据
        ChainPurchase purchase = chainPurchaseMapper.selectChainPurchaseById(chainPurchase.getId());

        if (null == purchase) {
            throw new ServiceException("采购数据不存在");
        }
        if (chainPurchase.getQualifiedQuantity() > purchase.getQuantity()) {
            throw new ServiceException("合格数量不能大于采购数量");
        }
        if (chainPurchase.getQualifiedQuantity() == purchase.getQuantity() && StringUtils.isNotEmpty(chainPurchase.getReturnReason())) {
            throw new ServiceException("全部合格时无需填写退货原因");
        }

        //只有送达之后才能质检
        if (chainPurchase.getTransportStatus() != 4) {
            throw new ServiceException("当前状态不可提交检验");
        }
        if (chainPurchase.getInspectionStatus() != 0) {
            throw new ServiceException("当前状态不可提交检验");
        }

        //校验合格数量合法性
        Long qualifiedQty = chainPurchase.getQualifiedQuantity();

        // 校验合格数量是否合法
        if (qualifiedQty == null || qualifiedQty < 0 || qualifiedQty > purchase.getQuantity()) {
            throw new ServiceException("合格数量需在 0 到 " + purchase.getQuantity() + " 之间");
        }

        // 3. 计算不合格数量
        Long unqualifiedQty = purchase.getQuantity() - qualifiedQty;

        // 4. 更新质检状态


        // 5. 设置质检信息
        purchase.setQualifiedQuantity(qualifiedQty);
        purchase.setInspectionTime(new Date());
        purchase.setInspectorId(ShiroUtils.getUserId());
        if (qualifiedQty.equals(purchase.getQuantity())) {
            purchase.setInspectionStatus(1);// 全部合格
        } else if (qualifiedQty >0) {
            purchase.setInspectionStatus(2);// 部分合格
        } else {
            purchase.setInspectionStatus(3);// 全部不合格
        }

        // 6. 合格商品入库
        if (qualifiedQty > 0) {
            iChainStockService.increaseStock(purchase.getCommodityId(), qualifiedQty);
        }

        // 7. 处理不合格商品
        if (unqualifiedQty > 0) {
            handleUnqualified(purchase, unqualifiedQty);
            // 重置运输状态并要求重新发货
            chainPurchase.setTransportStatus(1L);
            // 需要重新审核
            chainPurchase.setAuditType(0L);
        }

        chainPurchaseMapper.updateChainPurchase(purchase);
    }

    private void handleUnqualified(ChainPurchase purchase, Long unqualifiedQty) {
        // 1.生成退货记录（需新建退货表）
        // 2.通知供应商重新发货（可通过状态变更实现）
        // 示例：将采购单状态重置为待发货

        // 1. 获取供应商当前库存
        ChainCooperationCommodity cooperationCommodity = iChainCooperationCommodityService
                .selectChainCooperationCommodityById(purchase.getCooCommodityId());
        // 2. 计算实际可发货数量
        Long deliverableQty = Math.min(unqualifiedQty, cooperationCommodity.getQuantity());
        Long pendingQty = unqualifiedQty - deliverableQty;


//        iChainStockService.decreaseStock(purchase.getCommodityId(), unqualifiedQty);

        /*purchase.setTransportStatus(1L);
        chainPurchaseMapper.updateChainPurchase(purchase);

        // 实际应调用消息通知服务
        System.out.println("生成退货单，数量：" + unqualifiedQty);*/

    }

    private ChainCooperationCommodity checkCommodityInventory(ChainPurchase chainPurchase) {
        ChainCooperationCommodity cooperationCommodity = iChainCooperationCommodityService.selectChainCooperationCommodityById(chainPurchase.getCooCommodityId());
        if (null == cooperationCommodity) {
            throw new ServiceException("商品不存在");
        }
        if (cooperationCommodity.getQuantity() < chainPurchase.getQuantity()) {
            throw new ServiceException("商品库存不足");
        }
        return cooperationCommodity;
    }


    public void checkOperatePurchase(Long purchaseId) {
        ChainPurchase chainPurchase = chainPurchaseMapper.selectChainPurchaseById(purchaseId);
        if (null == chainPurchase) {
            throw new ServiceException("采购单不存在");
        }
        //如果审核通过
        if (chainPurchase.getAuditType() == 3) {
            throw new ServiceException("采购单已审核通过，不允许操作");
        }
    }

    public List<Map> selectBestPurchase() {
        return chainPurchaseMapper.selectBestPurchase();
    }

    public List<Map> selectPurchaseStatistics() {
        return chainPurchaseMapper.selectPurchaseStatistics();
    }

    public List<Map> selectPurchasetTansportStatus() {
        return chainPurchaseMapper.selectPurchasetTansportStatus();
    }
}
